<?php
include "../mer.php";

$mer = new MER();

?>
<html>
<head>
<script type="text/javascript" src="<?php print $mer->config->http_root; ?>/js/jquery-1.3.2.min.js"></script>
<link rel="stylesheet" href="<?php print $mer->config->http_root; ?>/css/admin.css" type="text/css" />

</head>
<body>

<h1>MER: Admin</h1>

<?php
$mer->check_requirements();
if(isset($_REQUEST["save_fields"])){
	$mer->save_fields();
}

$mer->get_search_fields();
list($mer_PID, $mer_log) = $mer->get_PID();

if(isset($_REQUEST["harvest_action"])){
	if(isset($_REQUEST["harvest"])){
		if($mer_PID){
			print "<div class='red_message'>Harvester currently running with PID $mer_PID. The harvester log can be found at $mer_log</div>";
		}else{
			$mer->harvest();
			list($mer_PID, $mer_log) = $mer->get_PID();
		}
		$mer->save_threshold();
	}elseif(isset($_REQUEST["clear"])){
		$mer->clear_terms();
	}elseif(isset($_REQUEST["save_threshold"])){
		$mer->save_threshold();
	}
}
$mer->get_threshold();

?>
<div id="build_search_form">
<h2>Build search form</h2>
<p>Select the fields you would like displayed in the left hand search panel. Choose 3-5 items as more will make for too tall a page.</p>

<form action="" method="post">
<?php
print $mer->get_EP_field_menu();
?>
<!-- <label for="repo_id">Repository ID: </label><input id="repo_id" name="repo_id" type="text"/> -->
<input type="hidden" name="save_fields" value="1"/>
<input type="submit" value="Save search fields"/>
</form>
</div>
<div id="curr_fields">Current search fields: <ul></ul></div>
<a href="<?php print $mer->config->http_root;?>/ui/">The MER search interface (<?php print $mer->config->repo_url.$mer->config->http_root;?>/ui/)</a>
<div id="harvester">
<h2>Harvester</h2>
<?php
list($terms, $assocs, $records_processed) = $mer->mer_stats();
?>
<form action="" method="post">
<table>
	<tbody>
		<tr>
			<th>Termine threshold: </th>
			<td><input type="text" name="threshold" style="width: 20px;" value="<?php print $mer->config->threshold;?>"/></td>
			<td><input type="submit" name="save_threshold" value="Save threshold" id="save_threshold"/></td>
		</tr>
		<tr>
			<th>Records processed: </th>
			<td><?php print $records_processed;?></td>
		</tr>
		<tr>
			<th>Terms identified: </th>
			<td><?php print $terms;?></td>
		</tr>
		<tr>
			<th>Associations made: </th>
			<td><?php print $assocs;?></td>
		</tr>
	</tbody>
</table>
<?php
if($mer_PID){
	print "<div class='green_message'>Harvester currently running with PID $mer_PID. The harvester log can be found at $mer_log</div>";
}
?>
<p>
	<input type="submit" name="harvest" value="Run MER term harvester" id="run_harvester"/>
	<input type="submit" name="clear" value="Delete terms" id="clear_terms"/>
</p>
<input type="hidden" name="harvest_action" value="1"/>
</form>
</div>


</body>
<script type="text/javascript">
$(document).ready(function(){
	<?php foreach($mer->sf as $sf=>$v){ ?>
		$("#<?php print $sf; ?>").attr("checked", "on");
		$("#curr_fields ul").append('<li>'+$("#<?php print $sf; ?>").parent("li").text()+'</li>');
	<?php }?>
});
</script>

</html>
